home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13155 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Recursion
  5. Date: Thu, 04 Apr 96 22:43:30 GMT
  6. Organization: none
  7. Message-ID: <828657810snz@genesis.demon.co.uk>
  8. References: <31624BC2.70D2@sooner.net> <828548265snz@genesis.demon.co.uk> <4k10q0$m5d@linet06.li.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4k10q0$m5d@linet06.li.net>
  15.            jeremy@newshost.li.net "Jeremy Markman" writes:
  16.  
  17. >Lawrence Kirby (fred@genesis.demon.co.uk) wrote:
  18. >
  19. >: I think you've picked a bad example. You really need an extra argument in
  20. >: this case to pass on a running accumulator. A more suitable problem would
  21. >: be to write a recursive function that is passed an integer (unsigned is
  22. >: easiest) and writes the character representation to stdout.
  23. >Absolutely WRONG.  The whole point of recursion is that you do not need a 
  24. >running accumulator.
  25.  
  26. Recursion is a powerful method of formulating, expressing and
  27. indeed solving problems. Running accumulators are a useful and commonly used
  28. technique for dealing with such problems in recursion and functional
  29. languages.
  30.  
  31. > When written correctly, the return value of each 
  32. >recursive loop will be the correct accumulated value.  ANyhow, here is a 
  33. >better version of what I already posted...
  34.  
  35. It is a good idea to compile and test your code before posting it. What you
  36. posted before simply doesn't work (at least not the recursive version).
  37.  
  38. >#include <stdio.h>
  39. >#include <stdlib.h>
  40. >#include <string.h>
  41. >#include <math.h>
  42. >
  43. >int convert(char *string)
  44. > {
  45. >  int value;
  46. >  int len = strlen(string);
  47. >
  48. >  if (string[0] == '\0')
  49. >    return(0);
  50. >  value = convert(string+1);
  51. >  value += ((string[0] - '0') * pow(10,len-1));
  52. >  return(value);
  53. > }
  54.  
  55. Really you've proved my point. You've had to resort to an iterative scan
  56. of the string (strlen) which makes this algorithm O(N*N) on the string length.
  57. You've also had to use a heavyweight floating point function (pow) which
  58. may not give accurate results. Using an accumuator would have produced
  59. a far better algorithm and still be very much in the spirit of recursion.
  60.  
  61. OK, I accept that this problem can be solved without an accumulator but the
  62. lengths you've had to go to to do so are unreasonable, and certainly show
  63. recursion in an unfairly negative light.
  64.  
  65. >void main()
  66.  
  67. Results in undefined behavious - use:
  68.  
  69. int main(void)
  70.  
  71. > {
  72. >  char *string = "1234";
  73. >
  74. >  printf("%d\n",convert(string));
  75. > }
  76. >
  77.  
  78. -- 
  79. -----------------------------------------
  80. Lawrence Kirby | fred@genesis.demon.co.uk
  81. Wilts, England | 70734.126@compuserve.com
  82. -----------------------------------------
  83.